home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / bfd / archures.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-08  |  15.0 KB  |  704 lines

  1. /* BFD library support routines for architectures.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Hacked by John Gilmore and Steve Chamberlain of Cygnus Support.
  4.  
  5.  
  6. This file is part of BFD, the Binary File Descriptor library.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /*
  23.  
  24. SECTION
  25.     Architectures
  26.  
  27.     BFD's idea of an architecture is implimented in
  28.     <<archures.c>>. BFD keeps one atom in a BFD describing the
  29.     architecture of the data attached to the BFD;  a pointer to a
  30.     <<bfd_arch_info_type>>.  
  31.  
  32.     Pointers to structures can be requested independently of a bfd
  33.     so that an architecture's information can be interrogated
  34.     without access to an open bfd.
  35.  
  36.     The arch information is provided by each architecture package.
  37.     The set of default architectures is selected by the #define
  38.     <<SELECT_ARCHITECTURES>>.  This is normally set up in the
  39.     <<config\/h\->> file of your choice.  If the name is not
  40.     defined, then all the architectures supported are included. 
  41.  
  42.     When BFD starts up, all the architectures are called with an
  43.     initialize method.  It is up to the architecture back end to
  44.     insert as many items into the list of arches as it wants to,
  45.     generally this would be one for each machine and one for the
  46.     default case (an item with a machine field of 0). 
  47. */
  48.  
  49. /*
  50.  
  51. SUBSECTION
  52.     bfd_architecture
  53.  
  54. DESCRIPTION
  55.     This enum gives the object file's CPU architecture, in a
  56.     global sense. E.g. what processor family does it belong to?
  57.     There is another field, which indicates what processor within
  58.     the family is in use.  The machine gives a number which
  59.     distingushes different versions of the architecture,
  60.     containing for example 2 and 3 for Intel i960 KA and i960 KB,
  61.     and 68020 and 68030 for Motorola 68020 and 68030. 
  62.  
  63. .enum bfd_architecture 
  64. .{
  65. .  bfd_arch_unknown,   {* File arch not known *}
  66. .  bfd_arch_obscure,   {* Arch known, not one of these *}
  67. .  bfd_arch_m68k,      {* Motorola 68xxx *}
  68. .  bfd_arch_vax,       {* DEC Vax *}   
  69. .  bfd_arch_i960,      {* Intel 960 *}
  70. .    {* The order of the following is important.
  71. .       lower number indicates a machine type that 
  72. .       only accepts a subset of the instructions
  73. .       available to machines with higher numbers.
  74. .       The exception is the "ca", which is
  75. .       incompatible with all other machines except 
  76. .       "core". *}
  77. .
  78. .#define bfd_mach_i960_core      1
  79. .#define bfd_mach_i960_ka_sa     2
  80. .#define bfd_mach_i960_kb_sb     3
  81. .#define bfd_mach_i960_mc        4
  82. .#define bfd_mach_i960_xa        5
  83. .#define bfd_mach_i960_ca        6
  84. .
  85. .  bfd_arch_a29k,      {* AMD 29000 *}
  86. .  bfd_arch_sparc,     {* SPARC *}
  87. .  bfd_arch_mips,      {* MIPS Rxxxx *}
  88. .  bfd_arch_i386,      {* Intel 386 *}
  89. .  bfd_arch_ns32k,     {* National Semiconductor 32xxx *}
  90. .  bfd_arch_tahoe,     {* CCI/Harris Tahoe *}
  91. .  bfd_arch_i860,      {* Intel 860 *}
  92. .  bfd_arch_romp,      {* IBM ROMP PC/RT *}
  93. .  bfd_arch_alliant,   {* Alliant *}
  94. .  bfd_arch_convex,    {* Convex *}
  95. .  bfd_arch_m88k,      {* Motorola 88xxx *}
  96. .  bfd_arch_pyramid,   {* Pyramid Technology *}
  97. .  bfd_arch_h8300,     {* Hitachi H8/300 *}
  98. .  bfd_arch_rs6000,    {* IBM RS/6000 *}
  99. .  bfd_arch_last
  100. .  };
  101.  
  102.  
  103. */
  104.  
  105.  
  106.  
  107. /* $Id: archures.c,v 1.22 1991/12/08 00:55:40 sac Exp $ */
  108.  
  109. #include "bfd.h"
  110. #include "sysdep.h"
  111. #include "libbfd.h"
  112.  
  113. /*
  114.  
  115. SUBSECTION
  116.     bfd_arch_info
  117.  
  118. DESCRIPTION
  119.     This structure contains information on architectures for use
  120.     within BFD.
  121.  
  122. .typedef int bfd_reloc_code_type;
  123. .
  124. .typedef struct bfd_arch_info 
  125. .{
  126. .  int bits_per_word;
  127. .  int bits_per_address;
  128. .  int bits_per_byte;
  129. .  enum bfd_architecture arch;
  130. .  long mach;
  131. .  char *arch_name;
  132. .  CONST  char *printable_name;
  133. . {* true if this is the default machine for the architecture *}
  134. . unsigned int section_align_power;
  135. .  boolean the_default;    
  136. .  CONST struct bfd_arch_info * EXFUN((*compatible),
  137. .    (CONST struct bfd_arch_info *a,
  138. .     CONST struct bfd_arch_info *b));
  139. .
  140. .  boolean EXFUN((*scan),(CONST struct bfd_arch_info *,CONST char *));
  141. .  unsigned int EXFUN((*disassemble),(bfd_vma addr, CONST char *data,
  142. .                     PTR stream));
  143. .  CONST struct reloc_howto_struct *EXFUN((*reloc_type_lookup),
  144. .    (CONST struct bfd_arch_info *,
  145. .    bfd_reloc_code_type  code));
  146. .
  147. .  struct bfd_arch_info *next;
  148. .
  149. .} bfd_arch_info_type;
  150. */
  151.  
  152. bfd_arch_info_type   *bfd_arch_info_list;
  153.  
  154.  
  155. /*
  156. FUNCTION
  157.     bfd_printable_name
  158.  
  159. SYNOPSIS
  160.     CONST char *bfd_printable_name(bfd *abfd);
  161.  
  162. DESCRIPTION
  163.     Return a printable string representing the architecture and machine
  164.     from the pointer to the arch info structure 
  165.  
  166. */
  167.  
  168. CONST char *
  169. DEFUN(bfd_printable_name, (abfd),
  170.       bfd *abfd) 
  171. {
  172.   return abfd->arch_info->printable_name;
  173. }
  174.  
  175.  
  176.  
  177. /*
  178. FUNCTION
  179.     bfd_scan_arch
  180.  
  181. SYNOPSIS
  182.     bfd_arch_info_type *bfd_scan_arch(CONST char *);
  183.  
  184. DESCRIPTION
  185.     This routine is provided with a string and tries to work out
  186.     if bfd supports any cpu which could be described with the name
  187.     provided.  The routine returns a pointer to an arch_info
  188.     structure if a machine is found, otherwise NULL.
  189.  
  190. */
  191.  
  192. bfd_arch_info_type *
  193. DEFUN(bfd_scan_arch,(string),
  194.       CONST char *string)
  195. {
  196.   struct bfd_arch_info *ap;
  197.  
  198.   /* Look through all the installed architectures */
  199.   for (ap = bfd_arch_info_list;
  200.        ap != (bfd_arch_info_type *)NULL;
  201.        ap = ap->next) {
  202.  
  203.     if (ap->scan(ap, string)) 
  204.       return ap;
  205.   }
  206.   return (bfd_arch_info_type *)NULL;
  207. }
  208.  
  209.  
  210.  
  211. /*
  212. FUNCTION
  213.     bfd_arch_get_compatible
  214.  
  215. SYNOPSIS
  216.     CONST bfd_arch_info_type *bfd_arch_get_compatible(
  217.         CONST bfd *abfd,
  218.             CONST bfd *bbfd);
  219.  
  220. DESCRIPTION
  221.     This routine is used to determine whether two BFDs'
  222.     architectures and achine types are compatible.  It calculates
  223.     the lowest common denominator between the two architectures
  224.     and machine types implied by the BFDs and returns a pointer to
  225.     an arch_info structure describing the compatible machine.
  226. */
  227.  
  228. CONST bfd_arch_info_type *
  229. DEFUN(bfd_arch_get_compatible,(abfd, bbfd),
  230. CONST    bfd *abfd AND
  231. CONST    bfd *bbfd)
  232.  
  233. {
  234.   return  abfd->arch_info->compatible(abfd->arch_info,bbfd->arch_info);
  235. }
  236.  
  237.  
  238. /*
  239. INTERNAL_DEFINITION
  240.     bfd_default_arch_struct
  241.  
  242. DESCRIPTION
  243.     The <<bfd_default_arch_struct>> is an item of
  244.     <<bfd_arch_info_type>> which has been initialized to a fairly
  245.     generic state.  A BFD starts life by pointing to this
  246.     structure, until the correct back end has determined the real
  247.     architecture of the file.
  248.  
  249. .extern bfd_arch_info_type bfd_default_arch_struct;
  250.  
  251. */
  252.  
  253. bfd_arch_info_type bfd_default_arch_struct =
  254. {
  255.     32,32,8,bfd_arch_unknown,0,"unknown","unknown",1,true,
  256.     bfd_default_compatible,
  257.     bfd_default_scan, 
  258.     0,
  259.     bfd_default_reloc_type_lookup
  260.  
  261. };
  262.  
  263. /*
  264. FUNCTION
  265.     bfd_set_arch_info
  266.  
  267. SYNOPSIS
  268.     void bfd_set_arch_info(bfd *, bfd_arch_info_type *);
  269.  
  270. */
  271.  
  272. void DEFUN(bfd_set_arch_info,(abfd, arg),
  273. bfd *abfd AND
  274. bfd_arch_info_type *arg)
  275. {
  276.   abfd->arch_info = arg;
  277. }
  278.  
  279. /*
  280. INTERNAL_FUNCTION
  281.     bfd_default_set_arch_mach
  282.  
  283. SYNOPSIS
  284.     boolean bfd_default_set_arch_mach(bfd *abfd,
  285.         enum bfd_architecture arch,
  286.         unsigned long mach);
  287.  
  288. DESCRIPTION
  289.     Set the architecture and machine type in a bfd. This finds the
  290.     correct pointer to structure and inserts it into the arch_info
  291.     pointer. 
  292. */
  293.  
  294. boolean DEFUN(bfd_default_set_arch_mach,(abfd, arch, mach),
  295.           bfd *abfd AND
  296.           enum bfd_architecture arch AND
  297.           unsigned    long mach)
  298. {
  299.   static struct bfd_arch_info *old_ptr = &bfd_default_arch_struct;
  300.   boolean found = false;
  301.   /* run through the table to find the one we want, we keep a little
  302.      cache to speed things up */
  303.   if (old_ptr == 0 || arch != old_ptr->arch || mach != old_ptr->mach) {
  304.     bfd_arch_info_type *ptr;
  305.     old_ptr = (bfd_arch_info_type *)NULL;
  306.     for (ptr = bfd_arch_info_list;
  307.      ptr != (bfd_arch_info_type *)NULL;
  308.      ptr= ptr->next) {
  309.       if (ptr->arch == arch &&
  310.       ((ptr->mach == mach) || (ptr->the_default && mach == 0))) {
  311.     old_ptr = ptr;
  312.     found = true;
  313.     break;
  314.       }
  315.     }
  316.     if (found==false) {
  317.       /*looked for it and it wasn't there, so put in the default */
  318.       old_ptr = &bfd_default_arch_struct;
  319.  
  320.     }
  321.   }
  322.   else {
  323.     /* it was in the cache */
  324.     found = true;
  325.   }
  326.  
  327.   abfd->arch_info = old_ptr;
  328.  
  329.   return found;
  330. }
  331.  
  332.  
  333.  
  334.  
  335.  
  336. /*
  337. FUNCTION
  338.     bfd_get_arch
  339.  
  340. SYNOPSIS
  341.     enum bfd_architecture bfd_get_arch(bfd *abfd);
  342.  
  343. DESCRIPTION
  344.     Returns the enumerated type which describes the supplied bfd's
  345.     architecture
  346.  
  347. */
  348.  
  349. enum bfd_architecture DEFUN(bfd_get_arch, (abfd), bfd *abfd)
  350. {
  351.     return abfd->arch_info->arch;
  352. }
  353.  
  354. /*
  355. FUNCTION
  356.     bfd_get_mach
  357.  
  358. SYNOPSIS
  359.     unsigned long bfd_get_mach(bfd *abfd);
  360.  
  361. DESCRIPTION
  362.     Returns the long type which describes the supplied bfd's
  363.     machine
  364. */
  365.  
  366. unsigned long  
  367. DEFUN(bfd_get_mach, (abfd), bfd *abfd)
  368. {
  369.     return abfd->arch_info->mach;
  370. }
  371.  
  372. /*
  373. FUNCTION
  374.     bfd_arch_bits_per_byte
  375.  
  376. SYNOPSIS
  377.     unsigned int bfd_arch_bits_per_byte(bfd *abfd);
  378.  
  379. DESCRIPTION
  380.     Returns the number of bits in one of the architectures bytes
  381.  
  382. */
  383.  
  384. unsigned int DEFUN(bfd_arch_bits_per_byte, (abfd), bfd *abfd)
  385.   {
  386.     return abfd->arch_info->bits_per_byte;
  387.   }
  388.  
  389. /*
  390. FUNCTION
  391.     bfd_arch_bits_per_address
  392.  
  393. SYNOPSIS
  394.     unsigned int bfd_arch_bits_per_address(bfd *abfd);
  395.  
  396. DESCRIPTION
  397.     Returns the number of bits in one of the architectures addresses
  398. */
  399.  
  400. unsigned int DEFUN(bfd_arch_bits_per_address, (abfd), bfd *abfd)
  401.   {
  402.     return abfd->arch_info->bits_per_address;
  403.   }
  404.  
  405.  
  406.  
  407. extern void EXFUN(bfd_h8300_arch,(void));
  408. extern void EXFUN(bfd_i960_arch,(void));
  409. extern void EXFUN(bfd_empty_arch,(void));
  410. extern void EXFUN(bfd_sparc_arch,(void));
  411. extern void EXFUN(bfd_m88k_arch,(void));
  412. extern void EXFUN(bfd_m68k_arch,(void));
  413. extern void EXFUN(bfd_vax_arch,(void));
  414. extern void EXFUN(bfd_a29k_arch,(void));
  415. extern void EXFUN(bfd_mips_arch,(void));
  416. extern void EXFUN(bfd_i386_arch,(void));
  417. extern void EXFUN(bfd_rs6000_arch,(void));
  418.  
  419.  
  420.  
  421. static void EXFUN((*archures_init_table[]),()) = 
  422. {
  423. #ifdef SELECT_ARCHITECTURES
  424.   SELECT_ARCHITECTURES,
  425. #else
  426.   bfd_sparc_arch,
  427.   bfd_a29k_arch,
  428.   bfd_mips_arch,
  429.   bfd_h8300_arch,
  430.   bfd_i386_arch,
  431.   bfd_m88k_arch,
  432.   bfd_i960_arch,
  433.   bfd_m68k_arch,
  434.   bfd_vax_arch,
  435.   bfd_rs6000_arch,
  436. #endif
  437.   0
  438.   };
  439.  
  440.  
  441.  
  442. /*
  443. INTERNAL_FUNCTION 
  444.     bfd_arch_init
  445.  
  446. SYNOPSIS
  447.     void  bfd_arch_init(void);
  448.  
  449. DESCRIPTION
  450.     This routine initializes the architecture dispatch table by
  451.     calling all installed architecture packages and getting them
  452.     to poke around.
  453. */
  454.  
  455. void
  456. DEFUN_VOID(bfd_arch_init)
  457. {
  458.     void EXFUN((**ptable),());
  459.     for (ptable = archures_init_table; 
  460.      *ptable ;
  461.      ptable++)
  462.     {
  463.     (*ptable)();
  464.     }
  465. }
  466.  
  467.  
  468. /*
  469. INTERNAL_FUNCTION
  470.     bfd_arch_linkin
  471.  
  472. SYNOPSIS
  473.     void bfd_arch_linkin(bfd_arch_info_type *);
  474.  
  475. DESCRIPTION
  476.     Link the provided arch info structure into the list
  477. */
  478.  
  479. void DEFUN(bfd_arch_linkin,(ptr),
  480.        bfd_arch_info_type *ptr)
  481. {
  482.   ptr->next = bfd_arch_info_list;
  483.   bfd_arch_info_list = ptr;
  484. }
  485.  
  486.  
  487. /*
  488. INTERNAL_FUNCTION 
  489.     bfd_default_compatible
  490.  
  491. SYNOPSIS
  492.     CONST bfd_arch_info_type *bfd_default_compatible
  493.     (CONST bfd_arch_info_type *a,
  494.     CONST bfd_arch_info_type *b);
  495.  
  496. DESCRIPTION
  497.     The default function for testing for compatibility.
  498. */
  499.  
  500. CONST bfd_arch_info_type *
  501. DEFUN(bfd_default_compatible,(a,b),
  502.       CONST bfd_arch_info_type *a AND
  503.       CONST bfd_arch_info_type *b)
  504. {
  505.   if(a->arch != b->arch) return NULL;
  506.  
  507.   if (a->mach > b->mach) {
  508.     return a;
  509.   }
  510.   if (b->mach > a->mach) {
  511.     return b;
  512.   }
  513.   return a;
  514. }
  515.  
  516.  
  517. /*
  518. INTERNAL_FUNCTION
  519.     bfd_default_scan
  520.  
  521. SYNOPSIS
  522.     boolean bfd_default_scan(CONST struct bfd_arch_info *, CONST char *);
  523.  
  524. DESCRIPTION
  525.     The default function for working out whether this is an
  526.     architecture hit and a machine hit.
  527. */
  528.  
  529. boolean 
  530. DEFUN(bfd_default_scan,(info, string),
  531. CONST struct bfd_arch_info *info AND
  532. CONST char *string)
  533. {
  534.     CONST  char *ptr_src;
  535.     CONST   char *ptr_tst;
  536.     unsigned long number;
  537.     enum bfd_architecture arch;
  538.     /* First test for an exact match */
  539.     if (strcmp(string, info->printable_name) == 0) return true;
  540.  
  541.     /* See how much of the supplied string matches with the
  542.        architecture, eg the string m68k:68020 would match the 68k entry
  543.        up to the :, then we get left with the machine number */
  544.  
  545.     for (ptr_src = string,
  546.      ptr_tst = info->arch_name; 
  547.      *ptr_src && *ptr_tst;
  548.      ptr_src++,
  549.      ptr_tst++) 
  550.     {
  551.     if (*ptr_src != *ptr_tst) break;
  552.     }
  553.  
  554.     /* Chewed up as much of the architecture as will match, skip any
  555.        colons */
  556.     if (*ptr_src == ':') ptr_src++;
  557.   
  558.     if (*ptr_src == 0) {
  559.         /* nothing more, then only keep this one if it is the default
  560.            machine for this architecture */
  561.         return info->the_default;
  562.     }
  563.     number = 0;
  564.     while (isdigit(*ptr_src)) {
  565.         number = number * 10 + *ptr_src  - '0';
  566.         ptr_src++;
  567.     }
  568.  
  569.     switch (number) 
  570.     {
  571.       case 68010:
  572.       case 68020:
  573.       case 68030:
  574.       case 68040:
  575.       case 68332:
  576.       case 68050:        
  577.       case 68000: 
  578.     arch = bfd_arch_m68k; 
  579.     break;
  580.       case 386: 
  581.       case 80386:
  582.       case 486:
  583.     arch = bfd_arch_i386;
  584.     break;
  585.       case 29000: 
  586.     arch = bfd_arch_a29k;
  587.     break;
  588.  
  589.       case 32016:
  590.       case 32032:
  591.       case 32132:
  592.       case 32232:
  593.       case 32332:
  594.       case 32432:
  595.       case 32532:  
  596.       case 32000: 
  597.     arch = bfd_arch_ns32k; 
  598.     break;
  599.  
  600.       case 860:
  601.       case 80860: 
  602.     arch = bfd_arch_i860; 
  603.     break;
  604.  
  605.       case 6000:
  606.     arch = bfd_arch_rs6000;
  607.     break;
  608.  
  609.       default:  
  610.     return false;
  611.     }
  612.     if (arch != info->arch) 
  613.      return false;
  614.  
  615.     if (number != info->mach)
  616.      return false;
  617.  
  618.     return true;
  619. }
  620.  
  621.  
  622.  
  623.  
  624. /*
  625. FUNCTION
  626.     bfd_get_arch_info
  627.  
  628.  
  629. SYNOPSIS
  630.     bfd_arch_info_type * bfd_get_arch_info(bfd *);
  631.  
  632. */
  633.  
  634. bfd_arch_info_type *
  635. DEFUN(bfd_get_arch_info,(abfd),
  636. bfd *abfd)
  637. {
  638.   return  abfd->arch_info;
  639. }
  640.  
  641.  
  642. /*
  643. FUNCTION
  644.     bfd_lookup_arch
  645.  
  646. SYNOPSIS
  647.     bfd_arch_info_type *bfd_lookup_arch
  648.         (enum bfd_architecture
  649.         arch,
  650.         long machine);
  651.  
  652. DESCRIPTION
  653.     Look for the architecure info struct which matches the
  654.     arguments given. A machine of 0 will match the
  655.     machine/architecture structure which marks itself as the
  656.     default.
  657. */
  658.  
  659. bfd_arch_info_type * 
  660. DEFUN(bfd_lookup_arch,(arch, machine),
  661. enum bfd_architecture arch AND
  662. long machine)
  663. {
  664.     bfd_arch_info_type *ap;
  665.     bfd_check_init();  
  666.     for (ap = bfd_arch_info_list; 
  667.      ap !=  (bfd_arch_info_type *)NULL;
  668.      ap = ap->next) {
  669.         if (ap->arch == arch &&
  670.         ((ap->mach == machine) 
  671.          || (ap->the_default && machine == 0))) {
  672.             return ap;
  673.         }
  674.     }
  675.     return (bfd_arch_info_type *)NULL;
  676. }
  677.  
  678.  
  679.  
  680. /*
  681. FUNCTION
  682.     bfd_printable_arch_mach
  683.  
  684. SYNOPSIS
  685.     CONST char * bfd_printable_arch_mach
  686.         (enum bfd_architecture arch, unsigned long machine);
  687.  
  688. DESCRIPTION
  689.     Return a printable string representing the architecture and
  690.     machine type. 
  691.  
  692.     NB. The use of this routine is depreciated.
  693. */
  694.  
  695. CONST char *
  696. DEFUN(bfd_printable_arch_mach,(arch, machine),
  697.       enum bfd_architecture arch AND
  698.       unsigned long machine)
  699. {
  700.     bfd_arch_info_type *ap = bfd_lookup_arch(arch, machine);
  701.     if(ap) return ap->printable_name;
  702.     return "UNKNOWN!";
  703. }
  704.